package com.magicsoft.app.helper;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.widget.EditText;

public class SmsContent extends ContentObserver {

//	public static final String SMS_URI_INBOX = "content://sms/inbox";
	private Uri SMS_INBOX = Uri.parse("content://sms/inbox");
	private Context context = null;
	private String smsContent = "";
	private EditText verifyText = null;
//	private String tel = "10690129883358115";

	public SmsContent(Context context, Handler handler, EditText verifyText) {
		super(handler);
		this.context = context;
		this.verifyText = verifyText;
	}

	@Override
	public void onChange(boolean selfChange) {
		super.onChange(selfChange);
		
		ContentResolver contentResolver = context.getContentResolver();
		String[] projection = new String[] { "body" };// "_id", "address",
														// "person",, "date",
														// "type
		String where = " read= '0'";
		Cursor cursor = contentResolver.query(SMS_INBOX, projection, where, null,
				"date desc");
		
		
//		Cursor cursor = null;// 光标
//		// 读取收件箱中指定号码的短信
//		cursor = activity.managedQuery(Uri.parse(SMS_URI_INBOX), new String[] {
//				"_id", "address", "body", "read" }, "address=? and read=?",
//				new String[] { tel, "0" }, "date desc");
		if (cursor != null) {// 如果短信为未读模式
			cursor.moveToFirst();
			if (cursor.moveToFirst()) {
				String smsbody = cursor
						.getString(cursor.getColumnIndex("body"));
				
				System.out.println("----------------------->>>" + smsbody);
				
				if (smsbody.contains("【彩之云】")) {
					
					String regEx = "[^0-9]";
					Pattern p = Pattern.compile(regEx);
					Matcher m = p.matcher(smsbody.toString());
					smsContent = m.replaceAll("").trim().toString();
					verifyText.setText(smsContent);
				}
			}
		}
	}
}
